function t(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var i = t(require("../../utils/api2")), e = t(require("../../public/utils/api")), a = getApp();

Component({
    properties: {
        isShown: {
            type: Boolean,
            value: !0,
            observer: function(t) {
                this.setData({
                    isShown: t
                });
            }
        },
        styleConfig: {
            type: Object,
            observer: function(t) {
                var i = this.__formateClass(t);
                this.setData({
                    className: i
                });
            }
        },
        site: {
            type: Object,
            observer: function(t) {
                this.setData({
                    site: t
                });
                var i = t.sid;
                i && this.initRadarIM(i);
            }
        }
    },
    data: {
        isShown: !0,
        className: ""
    },
    methods: {
        triggerLogin: function(t) {
            var i = getCurrentPages();
            i[i.length - 1].setData({
                isLoginModalShown: !0
            });
        },
        initRadarIM: function(t) {
            var i = this;
            a.globalData.tokens.accesstoken && wx.getStorage({
                key: "viewSiteList",
                success: function(e) {
                    var a = e.data;
                    a.includes(t) || i.getIsFirstView(t).then(function(t) {
                        a.push(t), wx.setStorageSync("viewSiteList", a);
                    });
                },
                fail: function() {
                    i.getIsFirstView(t).then(function(t) {
                        var i = [];
                        i.push(t), wx.setStorageSync("viewSiteList", i);
                    });
                }
            });
        },
        getIsFirstView: function(t) {
            var e = this;
            return new Promise(function(s, n) {
                i.default.site.site.getisfirstviewsite({
                    data: {
                        sid: t
                    }
                }).then(function(i) {
                    if (i.isfirst) {
                        var n = a.globalData.tokens.uid;
                        e.setData({
                            uid: n
                        }), e.creatChat(+n);
                    }
                    s(t);
                });
            });
        },
        creatChat: function(t) {
            var i = this, a = this.data.site.uid, s = this.data.site.server.uid, n = s || a;
            +t !== (+s || +a) && e.default.chat.chat.create({
                data: {
                    touid: t,
                    uid: n
                }
            }).then(function(t) {
                var e = t.data, a = e.chatid;
                e.count;
                i.sendMsg(a, n);
            });
        },
        sendMsg: function(t, e) {
            var s = this, n = this.data.site, o = n.name, r = n.server.speech, u = a.globalData.tokens.uid;
            i.default.chat.message.send({
                data: {
                    chatid: t,
                    uid: e,
                    touid: +u,
                    app: "site",
                    content: {
                        type: 1,
                        content: r || "欢迎访问" + o
                    },
                    issetnotread: 0
                }
            }).then(function(i) {
                s.exitChatRoom(t), console.log(i);
            });
        },
        exitChatRoom: function(t) {
            i.default.chat.chat.exitchat({
                data: {
                    chatid: t
                }
            }).then(function(t) {
                console.log(t, "退出成功");
            });
        },
        __formateClass: function(t) {
            var i = t.app, e = t.isEditable, a = t.isInitTabList, s = "";
            return "site" !== i && "thirdparty" !== i || (s = "site" === i ? e ? a ? "withinmsg-box--all" : "withinmsg-box--withoutTab" : a ? "withinmsg-box--withoutEdit" : "withinmsg-box--withoutAll" : "withinmsg-box--withoutTab"), 
            s;
        }
    }
});